package com.byxy.provider;

import java.util.Map;

import org.apache.ibatis.jdbc.SQL;

import com.byxy.entity.User;

public class UserProvider {
	public String insertUser(Map<String, Object> map) {
		User u = (User) map.get("user");
		String sql = "insert into user(";
		String data = "(";
		if (u.getUserName() != null) {
			sql += "user_name";
			data += "'" + u.getUserName() + "'"+")";
		}
		sql += ") values";
		return sql + data;
	}

	public String update(User u) {
		return new SQL() {
			{
				UPDATE("user");
				if (u.getUserPassword() != null) {
					SET("user_password=#{userPassword}");
				}
				;
				if (u.getUserNick() != null) {
					SET("user_nick=#{userNick}");
				}
				;
				WHERE("user_id=#{userId}");
			}
		}.toString();
	}

	public String insert(User u) {
		return new SQL() {
			{
				INSERT_INTO("user");
				if (u.getUserName() != null) {
					VALUES("user_name", "#{userName}");
				}
				;
				if (u.getUserPassword() != null) {
					VALUES("user_password", "#{userPassword}");
				}
				;
				if (u.getUserNick() != null) {
					VALUES("user_nick", "#{userNick}");
				}
				;
				if (u.getUserType() != null) {
					VALUES("user_type", "#{userType}");
				}
				;
			}
		}.toString();
	}
	
	public String delete(Integer userId) {
		return new SQL() {
			{
				DELETE_FROM("user");
				WHERE("user_id=#{userId}");
			}
		}.toString();
	}
	
	public String select() {
		 return new SQL(){
	            {
	                SELECT("*");
	                FROM("user");
	            }
	        }.toString();
	}
}
